package cn.bdqn.dao.impl;

import cn.bdqn.dao.FlowerDao;
import cn.bdqn.pojo.Flower;
import cn.bdqn.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FlowerDaoImpl extends DBUtil implements FlowerDao {
    @Override
    public List<Flower> getAllFlowers() {
        List<Flower> list = new ArrayList<>();
        String sql = "select * from flower";
        try {
            pstm = getConn().prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                Flower f = new Flower(rs.getInt("id"),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getDouble(5),
                        rs.getString(6));
                list.add(f);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(conn, pstm, rs);
        }
        return list;
    }

    @Override
    public int addFlower(Flower f) {
        String sql = "insert into flower(name,anothername,property,price,production) values(?,?,?,?,?)";
        Object[] os = {f.getName(), f.getAnothername(), f.getProperty(), f.getPrice(), f.getProduction()};
        return updateAll(sql,os);
    }
}
